package com.hongyuan.sellcoach.ui.course.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.CustomViewModel;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.MyRepository;
import com.hongyuan.sellcoach.ui.course.activity.CalendarActivity;
import com.hongyuan.sellcoach.ui.course.activity.CourseListActivity;
import com.hongyuan.sellcoach.ui.course.activity.SalesGiveLessonActivity;
import com.hongyuan.sellcoach.ui.course.activity.SalesPerformanceActivity;
import com.hongyuan.sellcoach.ui.course.beans.MyCourseBeans;
import com.hongyuan.sellcoach.ui.course.beans.MyCourseTopBeans;

import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CourseViewMode extends CustomViewModel<MyRepository> {

    public ObservableField<MyCourseTopBeans> topBeans = new ObservableField<>();

    public CourseViewMode(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<CourseItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<CourseItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_course);

    //查看课程日历
    public BindingCommand goCalendar = new BindingCommand(() -> {
        startActivity(CalendarActivity.class);
    });

    //查看销售课程业绩
    public BindingCommand goSalesGiveLesson = new BindingCommand(() -> {
        startActivity(SalesPerformanceActivity.class);
    });

    //查看销售业绩
    public BindingCommand goSalesPerformance = new BindingCommand(() -> {
        startActivity(SalesGiveLessonActivity.class);
    });

    //查看我的授课列表
    public BindingCommand goMyCourse = new BindingCommand(() -> {
        startActivity(CourseListActivity.class);
    });

    @Override
    public int aloneRefreshCode() {
        return Constants.COURSE_INDEX;
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.COURSE_INDEX){
            MyCourseTopBeans topBean = (MyCourseTopBeans)dataBean;
            topBeans.set(topBean);
        }

        if(code == Constants.GET_COURSE_PRIVITE_LIST){
            List<MyCourseBeans.ListBean> list = ((MyCourseBeans)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(list != null && list.size() > 0){
                for(MyCourseBeans.ListBean bean : list){
                    CourseItemViewModel itemViewModel = new CourseItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }
        }


    }
}
